/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sxu.shadowdrawabledemo.slice;

import com.sxu.shadowdrawable.ShadowDrawable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import java.util.ArrayList;
import java.util.List;

/*******************************************************************************
 * Description:
 *
 * Author: Freeman
 *
 * Date: 2019/2/12
 *
 * Copyright: all rights reserved by Freeman.
 *******************************************************************************/
public class ListExampleSlice extends Fraction {
    private List<String> dataList = new ArrayList<>();
    private ListContainer containerlist;
    private static final int PROPORTION = 3;
    private static final int DIRECEION = 1;
    private static final int MARGIN_RIGHT = 25;
    private static final int MARGIN_LEFT = 35;
    private static final int SCROLLBARTHICKNESS = 12;
    private static final int DATALISTSIZE = 10;
    private static final int MINHEIGHT = 120;
    private static final int TEXT_SIZE = 26;
    private static final int SHAPERADIUS = 8;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        containerlist = new ListContainer(getContext());
        containerlist.setMarginRight(MARGIN_RIGHT);
        containerlist.setMarginLeft(MARGIN_LEFT);
        containerlist.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
        containerlist.enableScrollBar(DIRECEION, true);
        containerlist.setScrollbarThickness(SCROLLBARTHICKNESS);
        containerlist.setScrollbarColor(Color.GRAY);
        containerlist.setLongClickable(false);
        return containerlist;
    }

    @Override
    protected void onActive() {
        super.onActive();
        for (int i1 = 0; i1 < DATALISTSIZE; i1++) {
            dataList.add("Item_" + i1);
        }
        containerlist.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return dataList.size();
            }

            @Override
            public Object getItem(int i) {
                return dataList.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                Text text = new Text(getContext());
                int padding = DATALISTSIZE * PROPORTION;
                text.setPadding(padding,padding,padding,padding);
                text.setTextAlignment(TextAlignment.CENTER);
                text.setText(dataList.get(i));
                text.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
                text.setMinHeight(MINHEIGHT * PROPORTION);
                text.setTextColor(Color.WHITE);
                text.setTextSize(TEXT_SIZE);
                ShadowDrawable.setShadowDrawable(text, Color.getIntColor("#2979FF"), SHAPERADIUS * PROPORTION,
                        Color.getIntColor("#ee7C4DFF"), padding, 0, 0);
                return text;
            }
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }
}
